#ifndef MAINSERVER_H
#define MAINSERVER_H

#include "jsonanswer.h"

#include <QObject>
#include <QTcpServer>
#include <QHostAddress>
#include <QSharedPointer>

const size_t ADDRIPV6_SIZE = 16;
typedef std::array<uint8_t, ADDRIPV6_SIZE> Address;

class Resolver : public QObject
{
    Q_OBJECT
public:
    explicit Resolver(const QString&, quint16, QObject* parent = nullptr);

    void disableAPI();
    void disableWebPage();
    void disableResolv();

private slots:
    void slotNewUser();
    void slotReadyClient();

private:
    QTcpServer* m_tcpServer;
    QHostAddress m_address;
    quint16 m_port;

    void convertStrToRaw(const QString& str, Address& array);
    QString getBase32(const Address& rawAddr);
    QString decodeMeshToIP(const QString& meshname);
    std::string pickupStringForMeshname(std::string str);
    QString getAddress(const Address& rawAddr);

    void startPage(QSharedPointer<QTextStream>, bool);
    void startPageWithMessage(const QString &, QSharedPointer<QTextStream>, const QString&, bool);
    void processPage(const QString&, QSharedPointer<QTextStream>, bool);
    void toMeship(const QString&, QSharedPointer<QTextStream>, bool);
    void toIp(const QString&, QSharedPointer<QTextStream>, bool);
    void toRealResolv(const QString&, QSharedPointer<QTextStream>, bool);
    void toVersion(const QString&, QSharedPointer<QTextStream>, bool);

    bool m_NoApi;
    bool m_NoHttp;
    bool m_NoResolve;
};

#endif // MAINSERVER_H
